<?xml version="1.0" encoding="ascii"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>simplejson.encoder.JSONEncoder</title>
  <link rel="stylesheet" href="epydoc.css" type="text/css" />
  <script type="text/javascript" src="epydoc.js"></script>
</head>

<body bgcolor="white" text="black" link="blue" vlink="#204080"
      alink="#204080">
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

      <th class="navbar" width="100%"></th>
  </tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="100%">
      <span class="breadcrumbs">
        <a href="simplejson-module.html">Package&nbsp;simplejson</a> ::
        <a href="simplejson.encoder-module.html" onclick="show_private();">Module&nbsp;encoder</a> ::
        Class&nbsp;JSONEncoder
      </span>
    </td>
    <td>
      <table cellpadding="0" cellspacing="0">
        <!-- hide/show private -->
        <tr><td align="right"><span class="options">[<a href="javascript:void(0);" class="privatelink"
    onclick="toggle_private();">hide&nbsp;private</a>]</span></td></tr>
        <tr><td align="right"><span class="options"
            >[<a href="frames.html" target="_top">frames</a
            >]&nbsp;|&nbsp;<a href="simplejson.encoder.JSONEncoder-class.html"
            target="_top">no&nbsp;frames</a>]</span></td></tr>
      </table>
    </td>
  </tr>
</table>
<!-- ==================== CLASS DESCRIPTION ==================== -->
<h1 class="epydoc">Class JSONEncoder</h1><p class="nomargin-top"><span class="codelink"><a href="simplejson.encoder-pysrc.html#JSONEncoder">source&nbsp;code</a></span></p>
<pre class="base-tree">
object --+
         |
        <strong class="uidshort">JSONEncoder</strong>
</pre>

<hr />
<p>Extensible JSON &lt;http://json.org&gt; encoder for Python data 
  structures.</p>
  <p>Supports the following objects and types by default:</p>
  <p>+-------------------+---------------+ | Python            | JSON
  | +===================+===============+ | dict              | object
  | +-------------------+---------------+ | list, tuple       | array
  | +-------------------+---------------+ | str, unicode      | string
  | +-------------------+---------------+ | int, long, float  | number
  | +-------------------+---------------+ | True              | true
  | +-------------------+---------------+ | False             | false
  | +-------------------+---------------+ | None              | null
  | +-------------------+---------------+</p>
  <p>To extend this to recognize other objects, subclass and implement a 
  ``.default()`` method with another method that returns a serializable 
  object for ``o`` if possible, otherwise it should call the superclass 
  implementation (to raise ``TypeError``).</p>

<!-- ==================== INSTANCE METHODS ==================== -->
<a name="section-InstanceMethods"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td colspan="2" class="table-header">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr valign="top">
        <td align="left"><span class="table-header">Instance Methods</span></td>
        <td align="right" valign="top"
         ><span class="options">[<a href="#section-InstanceMethods"
         class="privatelink" onclick="toggle_private();"
         >hide private</a>]</span></td>
      </tr>
    </table>
  </td>
</tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="simplejson.encoder.JSONEncoder-class.html#__init__" class="summary-sig-name">__init__</a>(<span class="summary-sig-arg">self</span>,
        <span class="summary-sig-arg">skipkeys</span>=<span class="summary-sig-default">False</span>,
        <span class="summary-sig-arg">ensure_ascii</span>=<span class="summary-sig-default">True</span>,
        <span class="summary-sig-arg">check_circular</span>=<span class="summary-sig-default">True</span>,
        <span class="summary-sig-arg">allow_nan</span>=<span class="summary-sig-default">True</span>,
        <span class="summary-sig-arg">sort_keys</span>=<span class="summary-sig-default">False</span>,
        <span class="summary-sig-arg">indent</span>=<span class="summary-sig-default">None</span>,
        <span class="summary-sig-arg">separators</span>=<span class="summary-sig-default">None</span>,
        <span class="summary-sig-arg">encoding</span>=<span class="summary-sig-default"><code class="variable-quote">'</code><code class="variable-string">utf-8</code><code class="variable-quote">'</code></span>,
        <span class="summary-sig-arg">default</span>=<span class="summary-sig-default">None</span>)</span><br />
      Constructor for JSONEncoder, with sensible defaults.</td>
          <td align="right" valign="top">
            <span class="codelink"><a href="simplejson.encoder-pysrc.html#JSONEncoder.__init__">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr class="private">
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a name="_newline_indent"></a><span class="summary-sig-name">_newline_indent</span>(<span class="summary-sig-arg">self</span>)</span></td>
          <td align="right" valign="top">
            <span class="codelink"><a href="simplejson.encoder-pysrc.html#JSONEncoder._newline_indent">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr class="private">
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a name="_iterencode_list"></a><span class="summary-sig-name">_iterencode_list</span>(<span class="summary-sig-arg">self</span>,
        <span class="summary-sig-arg">lst</span>,
        <span class="summary-sig-arg">markers</span>=<span class="summary-sig-default">None</span>)</span></td>
          <td align="right" valign="top">
            <span class="codelink"><a href="simplejson.encoder-pysrc.html#JSONEncoder._iterencode_list">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr class="private">
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a name="_iterencode_dict"></a><span class="summary-sig-name">_iterencode_dict</span>(<span class="summary-sig-arg">self</span>,
        <span class="summary-sig-arg">dct</span>,
        <span class="summary-sig-arg">markers</span>=<span class="summary-sig-default">None</span>)</span></td>
          <td align="right" valign="top">
            <span class="codelink"><a href="simplejson.encoder-pysrc.html#JSONEncoder._iterencode_dict">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr class="private">
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a name="_iterencode"></a><span class="summary-sig-name">_iterencode</span>(<span class="summary-sig-arg">self</span>,
        <span class="summary-sig-arg">o</span>,
        <span class="summary-sig-arg">markers</span>=<span class="summary-sig-default">None</span>)</span></td>
          <td align="right" valign="top">
            <span class="codelink"><a href="simplejson.encoder-pysrc.html#JSONEncoder._iterencode">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr class="private">
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a name="_iterencode_default"></a><span class="summary-sig-name">_iterencode_default</span>(<span class="summary-sig-arg">self</span>,
        <span class="summary-sig-arg">o</span>,
        <span class="summary-sig-arg">markers</span>=<span class="summary-sig-default">None</span>)</span></td>
          <td align="right" valign="top">
            <span class="codelink"><a href="simplejson.encoder-pysrc.html#JSONEncoder._iterencode_default">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="simplejson.encoder.JSONEncoder-class.html#default" class="summary-sig-name">default</a>(<span class="summary-sig-arg">self</span>,
        <span class="summary-sig-arg">o</span>)</span><br />
      Implement this method in a subclass such that it returns a 
      serializable object for ``o``, or calls the base implementation (to 
      raise a ``TypeError``).</td>
          <td align="right" valign="top">
            <span class="codelink"><a href="simplejson.encoder-pysrc.html#JSONEncoder.default">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="simplejson.encoder.JSONEncoder-class.html#encode" class="summary-sig-name">encode</a>(<span class="summary-sig-arg">self</span>,
        <span class="summary-sig-arg">o</span>)</span><br />
      Return a JSON string representation of a Python data structure.</td>
          <td align="right" valign="top">
            <span class="codelink"><a href="simplejson.encoder-pysrc.html#JSONEncoder.encode">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="simplejson.encoder.JSONEncoder-class.html#iterencode" class="summary-sig-name">iterencode</a>(<span class="summary-sig-arg">self</span>,
        <span class="summary-sig-arg">o</span>)</span><br />
      Encode the given object and yield each string representation as 
      available.</td>
          <td align="right" valign="top">
            <span class="codelink"><a href="simplejson.encoder-pysrc.html#JSONEncoder.iterencode">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
  <tr>
    <td colspan="2" class="summary">
    <p class="indent-wrapped-lines"><b>Inherited from <code>object</code></b>:
      <code>__delattr__</code>,
      <code>__getattribute__</code>,
      <code>__hash__</code>,
      <code>__new__</code>,
      <code>__reduce__</code>,
      <code>__reduce_ex__</code>,
      <code>__repr__</code>,
      <code>__setattr__</code>,
      <code>__str__</code>
      </p>
    </td>
  </tr>
</table>
<!-- ==================== CLASS VARIABLES ==================== -->
<a name="section-ClassVariables"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td colspan="2" class="table-header">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr valign="top">
        <td align="left"><span class="table-header">Class Variables</span></td>
        <td align="right" valign="top"
         ><span class="options">[<a href="#section-ClassVariables"
         class="privatelink" onclick="toggle_private();"
         >hide private</a>]</span></td>
      </tr>
    </table>
  </td>
</tr>
<tr class="private">
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a name="__all__"></a><span class="summary-name">__all__</span> = <code title="['__init__', 'default', 'encode', 'iterencode']"><code class="variable-group">[</code><code class="variable-quote">'</code><code class="variable-string">__init__</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">default</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">encode</code><code class="variable-quote">'</code><code class="variable-op">, </code><code class="variable-quote">'</code><code class="variable-string">iterencode</code><code class="variable-quote">'</code><code class="variable-group">]</code></code>
    </td>
  </tr>
<tr class="private">
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a name="item_separator"></a><span class="summary-name">item_separator</span> = <code title="', '"><code class="variable-quote">'</code><code class="variable-string">, </code><code class="variable-quote">'</code></code>
    </td>
  </tr>
<tr class="private">
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
        <a name="key_separator"></a><span class="summary-name">key_separator</span> = <code title="': '"><code class="variable-quote">'</code><code class="variable-string">: </code><code class="variable-quote">'</code></code>
    </td>
  </tr>
</table>
<!-- ==================== PROPERTIES ==================== -->
<a name="section-Properties"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td colspan="2" class="table-header">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr valign="top">
        <td align="left"><span class="table-header">Properties</span></td>
        <td align="right" valign="top"
         ><span class="options">[<a href="#section-Properties"
         class="privatelink" onclick="toggle_private();"
         >hide private</a>]</span></td>
      </tr>
    </table>
  </td>
</tr>
  <tr>
    <td colspan="2" class="summary">
    <p class="indent-wrapped-lines"><b>Inherited from <code>object</code></b>:
      <code>__class__</code>
      </p>
    </td>
  </tr>
</table>
<!-- ==================== METHOD DETAILS ==================== -->
<a name="section-MethodDetails"></a>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td colspan="2" class="table-header">
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr valign="top">
        <td align="left"><span class="table-header">Method Details</span></td>
        <td align="right" valign="top"
         ><span class="options">[<a href="#section-MethodDetails"
         class="privatelink" onclick="toggle_private();"
         >hide private</a>]</span></td>
      </tr>
    </table>
  </td>
</tr>
</table>
<a name="__init__"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">__init__</span>(<span class="sig-arg">self</span>,
        <span class="sig-arg">skipkeys</span>=<span class="sig-default">False</span>,
        <span class="sig-arg">ensure_ascii</span>=<span class="sig-default">True</span>,
        <span class="sig-arg">check_circular</span>=<span class="sig-default">True</span>,
        <span class="sig-arg">allow_nan</span>=<span class="sig-default">True</span>,
        <span class="sig-arg">sort_keys</span>=<span class="sig-default">False</span>,
        <span class="sig-arg">indent</span>=<span class="sig-default">None</span>,
        <span class="sig-arg">separators</span>=<span class="sig-default">None</span>,
        <span class="sig-arg">encoding</span>=<span class="sig-default"><code class="variable-quote">'</code><code class="variable-string">utf-8</code><code class="variable-quote">'</code></span>,
        <span class="sig-arg">default</span>=<span class="sig-default">None</span>)</span>
    <br /><em class="fname">(Constructor)</em>
  </h3>
  </td><td align="right" valign="top"
    ><span class="codelink"><a href="simplejson.encoder-pysrc.html#JSONEncoder.__init__">source&nbsp;code</a></span>&nbsp;
    </td>
  </tr></table>
  
  <p>Constructor for JSONEncoder, with sensible defaults.</p>
  <p>If skipkeys is False, then it is a TypeError to attempt encoding of 
  keys that are not str, int, long, float or None.  If skipkeys is True, 
  such items are simply skipped.</p>
  <p>If ensure_ascii is True, the output is guaranteed to be str objects 
  with all incoming unicode characters escaped.  If ensure_ascii is false, 
  the output will be unicode object.</p>
  <p>If check_circular is True, then lists, dicts, and custom encoded 
  objects will be checked for circular references during encoding to 
  prevent an infinite recursion (which would cause an OverflowError). 
  Otherwise, no such check takes place.</p>
  <p>If allow_nan is True, then NaN, Infinity, and -Infinity will be 
  encoded as such.  This behavior is not JSON specification compliant, but 
  is consistent with most JavaScript based encoders and decoders. 
  Otherwise, it will be a ValueError to encode such floats.</p>
  <p>If sort_keys is True, then the output of dictionaries will be sorted 
  by key; this is useful for regression tests to ensure that JSON 
  serializations can be compared on a day-to-day basis.</p>
  <p>If indent is a non-negative integer, then JSON array elements and 
  object members will be pretty-printed with that indent level.  An indent 
  level of 0 will only insert newlines. None is the most compact 
  representation.</p>
  <p>If specified, separators should be a (item_separator, key_separator) 
  tuple.  The default is (', ', ': ').  To get the most compact JSON 
  representation you should specify (',', ':') to eliminate whitespace.</p>
  <p>If specified, default is a function that gets called for objects that 
  can't otherwise be serialized.  It should return a JSON encodable version
  of the object or raise a ``TypeError``.</p>
  <p>If encoding is not None, then all input strings will be transformed 
  into unicode using that encoding prior to JSON-encoding. The default is 
  UTF-8.</p>
  <dl class="fields">
    <dt>Overrides:
        object.__init__
    </dt>
  </dl>
</td></tr></table>
</div>
<a name="default"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">default</span>(<span class="sig-arg">self</span>,
        <span class="sig-arg">o</span>)</span>
  </h3>
  </td><td align="right" valign="top"
    ><span class="codelink"><a href="simplejson.encoder-pysrc.html#JSONEncoder.default">source&nbsp;code</a></span>&nbsp;
    </td>
  </tr></table>
  
  <p>Implement this method in a subclass such that it returns a 
  serializable object for ``o``, or calls the base implementation (to raise
  a ``TypeError``).</p>
  <p>For example, to support arbitrary iterators, you could implement 
  default like this:</p>
<pre class="literalblock">
   def default(self, o):
       try:
           iterable = iter(o)
       except TypeError:
           pass
       else:
           return list(iterable)
       return JSONEncoder.default(self, o)
</pre>
  <dl class="fields">
  </dl>
</td></tr></table>
</div>
<a name="encode"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">encode</span>(<span class="sig-arg">self</span>,
        <span class="sig-arg">o</span>)</span>
  </h3>
  </td><td align="right" valign="top"
    ><span class="codelink"><a href="simplejson.encoder-pysrc.html#JSONEncoder.encode">source&nbsp;code</a></span>&nbsp;
    </td>
  </tr></table>
  
  <p>Return a JSON string representation of a Python data structure.</p>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span>JSONEncoder().encode({<span class="py-string">&quot;foo&quot;</span>: [<span class="py-string">&quot;bar&quot;</span>, <span class="py-string">&quot;baz&quot;</span>]})
<span class="py-output">'{&quot;foo&quot;: [&quot;bar&quot;, &quot;baz&quot;]}'</span></pre>
  <dl class="fields">
  </dl>
</td></tr></table>
</div>
<a name="iterencode"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">iterencode</span>(<span class="sig-arg">self</span>,
        <span class="sig-arg">o</span>)</span>
  </h3>
  </td><td align="right" valign="top"
    ><span class="codelink"><a href="simplejson.encoder-pysrc.html#JSONEncoder.iterencode">source&nbsp;code</a></span>&nbsp;
    </td>
  </tr></table>
  
  <p>Encode the given object and yield each string representation as 
  available.</p>
  <p>For example:</p>
<pre class="literalblock">
   for chunk in JSONEncoder().iterencode(bigobject):
       mysocket.write(chunk)
</pre>
  <dl class="fields">
  </dl>
</td></tr></table>
</div>
<br />
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

      <th class="navbar" width="100%"></th>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
  <tr>
    <td align="left" class="footer">
    Generated by Epydoc 3.0.1 on Thu Jan 29 04:00:46 2009
    </td>
    <td align="right" class="footer">
      <a target="mainFrame" href="http://epydoc.sourceforge.net"
        >http://epydoc.sourceforge.net</a>
    </td>
  </tr>
</table>

<script type="text/javascript">
  <!--
  // Private objects are initially displayed (because if
  // javascript is turned off then we want them to be
  // visible); but by default, we want to hide them.  So hide
  // them unless we have a cookie that says to show them.
  checkCookie();
  // -->
</script>
</body>
</html>
